package com.supcalls.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supcalls.entity.Storage;
import com.supcalls.mapper.StorageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

@Service
@Slf4j
public class StorageService {
    @Autowired
    private StorageMapper storageMapper;

    @Transactional
    public void decrease(Long productId, Integer count) {
        log.info("扣减库存, productId={}, count={}", productId, count);
        Storage storage = storageMapper.selectOne(new QueryWrapper<Storage>().eq("product_id", productId));
        storage.setUsed(storage.getUsed() + count);
        storage.setResidue(storage.getResidue() - count);
        storageMapper.updateById(storage);
    }
}

